package com.hh.store.controller;

import com.hh.store.VO.*;
import com.hh.store.service.StoreService;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Hanh
 * @date 2023/8/16 15:57s
 */
@RestController
@RequestMapping("store")
@CrossOrigin
public class StoreController {

    @Autowired
    private StoreService storeService;

    /**
     * 分页查询
     * @param queryStorePageVo 接收前端给的分页查询信息
     * @return
     */
    @PostMapping("queryPage")
    private ResultVo queryStorePage(@RequestBody QueryStorePageVo queryStorePageVo){
        return storeService.queryStorePage(queryStorePageVo);
    }

    /**
     * 根据id去启用禁用
     * @param statusVo
     * @param bindingResult
     * @return
     */
    @PostMapping("updateStatus")
    private ResultVo updateStatus(@RequestBody UpdateStatusVo statusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return storeService.updateStatus(statusVo);
    }

    /**
     * 添加库位
     * @param addStoreVo
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    private ResultVo addStore(@RequestBody @Valid AddStoreVo addStoreVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return storeService.addStore(addStoreVo);
    }


    /**
     * 修改库位
     * @param updateStoreVo
     * @param bindingResult
     * @return
     */
    @PostMapping("update")
    private ResultVo updateStore(@RequestBody UpdateStoreVo updateStoreVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return storeService.updateStore(updateStoreVo);
    }

    @GetMapping("remove")
    private ResultVo removeStore(@RequestParam("code")String code){

        return storeService.removeStore(code);
    }

}
